import Vue from 'vue'
import axios from 'axios'
import router from '@/router'

axios.defaults.baseURL = window.config.baseUrl

axios.interceptors.request.use(async function (config) {
  return config
}, function (error) {
  return Promise.reject(error)
})

axios.interceptors.response.use(function (response) {
  let statusCode = response.data.rescode ? response.data.rescode : response.data.statusCode
  switch (statusCode) {
    case '200':
      return response
    default:
      return response
      // Vue.bus.emit('showMessage', {
      //   msg: response.data.errInfo,
      //   type: 'error'
      // })
  }
}, function (error) {
  console.log(error)
})

Vue.prototype.http = axios

router.beforeEach(async (to, from, next) => {
  next()
})
export default axios
